import promise from "es6-promise";

promise.polyfill();
import axios from "axios"; //https://www.kancloud.cn/yunye/axios/234845
import Vue from "vue";
// import store from '../store/index';

axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "/api";
axios.defaults.timeout = 5000;
// 请求拦截器
axios.interceptors.request.use(
  config => {
    // 登陆验证
    // config.headers.token =
    //   "21232f297a57a5a743894a0e4a801fc3:1:c9122ed1ef59176c44068ed9521170a7"; // store.getters.getTokenInfo;
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
axios.interceptors.response.use(
  response => {
    console.log("response.data", response.data);
    if (response && response.data && response.data.code !== 200) {
      response.data.msg && new Vue().$notifyFailed(response.data.msg);
      return Promise.reject(response.data);
    }
    // new Vue().$notifyFailed("test");
    return response.data;
  },
  error => {
    if (error && error.response && error.response.status) {
      return Promise.reject(error);
    }
  }
);

export default axios;
